package com.project.service.imp;

import com.project.bean.ClassBean;
import com.project.mapper.IClassMapper;
import com.project.service.IClassService;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;

public class ClassServiceImp extends BaseService implements IClassService {
    @Override
    public ArrayList<ClassBean> showAllClass() {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        ArrayList<ClassBean> list = mapper.showAllClass();
        session.close();
        return list;
    }

    @Override
    public boolean addClass(ClassBean classBean) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        ClassBean classBean1 = mapper.selectClassByName(classBean.getClassName());
        if(classBean1 == null){
            mapper.addClass(classBean);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }


    @Override
    public boolean delClass(Integer classId) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        Integer num = mapper.stuNumByClassId(classId);
        if(num==0){
            mapper.delClass(classId);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }
}
